//
//  StatusViewModel.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/22.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

import SDWebImage

/// 结构体

struct GXLStatusCellPicture {
    //计算配图视图大小
    let margin:CGFloat = 12
    
    let pictureMargin:CGFloat = 3
    
    let pictureRow:CGFloat = 3
    
    let pictureViewWidth:CGFloat
    let pictureSize:CGSize
    init(){
    
     pictureViewWidth = UIScreen.mainScreen().bounds.width - 2*margin
    
    let pictureWH = (pictureViewWidth - (pictureRow-1)*2)/pictureRow
        
        pictureSize = CGSizeMake(pictureWH, pictureWH)
    
    }
}
//全局布局cell的结构体信息
let StatusCellLayout = GXLStatusCellPicture()


class StatusViewModel {
    
    var status: StatusModel
    /// 使用微博模型实例化视图模型
    
    //MARK: - 用户数据
    /// 图片数据 如果转发有图片则返回转发图片 否则返回原创
    var pictureUrl: [StatusPicturesModel]?{
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    /// 会员图标
   var memberIconImage: UIImage?
    /// 认证图标
    var vipIcionImage: UIImage?
    /// 来源字符串
    var sourceText: String?
    /// 创建日期
    var  dateTime: NSDate?
    /// 配图视图大小 默认无大小 原创
    var pictureViewSize = CGSizeZero
    /// 被转发微博视图大小
    var retweetedPictureViewSize = CGSizeZero
    /// 被转发微博文本
    var retWeedtedText: String?{
        
        if status.retweeted_status == nil {
            
            return nil
        }
        
        //拼接字符串
        return "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
    }
   
   
    init(status: StatusModel){
        
        self.status = status
        
        memberUserData()
        
        StatusDatda()
        
        pictureViewSize = calPictureView(status.pic_urls)
        
        retweetedPictureViewSize = calPictureView(status.retweeted_status?.pic_urls)
    }
    
    //MARK - 公共函数
   /// 更新单张图片视图的大小
    func updataSinglePictureViewSize(){
        
        pictureViewSize = calSinglePictureViewSize(status.pic_urls)
        
        retweetedPictureViewSize = calSinglePictureViewSize(status.retweeted_status?.pic_urls)
        
        
    }
    //MARK - 私有函数
    
    private func calSinglePictureViewSize(pic_urls: [StatusPicturesModel]?) -> CGSize{
        
        if pic_urls?.count != 1{
            
            return CGSizeZero
        }
        
        //获取url字符串
        let urlString = pic_urls![0].thumbnail_pic
        
        //使用SDWebImage 的缓存函数 key = url 的完整字符串
        
        guard let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlString) else {
            
            return CGSizeZero
        }
        //提示 SDWebImage 从3.7.4开始 会将网络返回的图像自动按照当前设备的分辨率转换
        //恢复图像chicun
        let scale = UIScreen.mainScreen().scale
         var size = CGSize(width: image.size.width*scale, height: image.size.width*scale)
        
        let midWidth: CGFloat = (UIScreen.mainScreen().bounds.width - 2*StatusCellLayout.margin)*0.5
        
        let minWidth: CGFloat = 25
        
        size.width = size.width < minWidth ? midWidth : 150
        
        size.height = size.width * image.size.height / image.size.width
        
        if size.height > 200 {
            
            size.height = 200
        }
      
       return size
    }
    
    private func calPictureView(pic_urls: [StatusPicturesModel]?) -> CGSize{
        //1判断图片数量
        let count = pic_urls?.count ?? 0
        
        //2如果没有图直接返回cgsizezero
        if count == 0 {
            
            return CGSizeZero
        }
        
        //记录布局属性
        let layout = StatusCellLayout
        
        //计算行数
        
        let row = CGFloat((count - 1)/Int(layout.pictureRow) + 1)
        
        //计算高度
        
        let height = row*(layout.pictureSize.height) + (row - 1)*layout.pictureMargin
        
//        let width = CGFloat(count)*pictureWH
        
        return CGSize(width:layout.pictureViewWidth, height: height)
        
        
    }
    
    
    private func StatusDatda(){
        /// 来源字符串
        //<a href=\"http://app.weibo.com/t/feed/5yiHuw\" rel=\"nofollow\">iPhone 6 Plus</a>
        
        if let source = status.source ,
            
            startIndex = source.rangeOfString("\">")?.endIndex,
            endIndex = source.rangeOfString("</a>")?.startIndex{
                
           sourceText = "来自" + source.substringWithRange(startIndex ..< endIndex)
        }
        
       /// 创建日期
        
        dateTime = NSDate.xl_sinaDate(status.created_at)
        
        
        
        
        
    }
    
    
    
   /// 计算数据
    private func memberUserData(){
        
        if status.user?.mbrank>0 && status.user?.mbrank<7{
            
            memberIconImage = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
        switch status.user?.verified_type ?? -1 {
            
        case 0: vipIcionImage = UIImage(named: "avatar_vip")
        case 2,3,5: vipIcionImage = UIImage(named: "avatar_enterprise_vip")
        case 220: vipIcionImage = UIImage(named: "avatar_grassroot")
            
            
        default: break
            
        }
        

        
        
        
        
        
    }
    
}